﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Core;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using GroceryPriceBook.Core.Service;
using GroceryPriceBook.Core.ViewModel;
using GroceryPriceBook.Core;
using GroceryPriceBook.Extensions;

namespace GroceryPriceBook.Controllers
{
	public class ProductCategoryController : BaseController
	{
		readonly ProductCategoryHandler _handler = new ProductCategoryHandler();

		// GET: /ProductCategory/
		public ActionResult Index()
		{
			return View(_handler.ReadAll());
		}

		// GET: /ProductCategory/Details/5
		public ActionResult Details(Guid? id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			var productcategory = _handler.ReadOne(id.Value);
			if (productcategory == null)
			{
				return HttpNotFound();
			}
			return View(productcategory);
		}

		// GET: /ProductCategory/Create
		public ActionResult Create()
		{
			var productCategory = new ProductCategory { ProductCategories = _handler.ReadAll() };
			productCategory.ProductCategorySelectListItems =
				productCategory.ProductCategories.ToSelectListItems(category => category.Name, category => category.Id.ToString(), category => category.Id == Guid.Empty);
			return View(productCategory);
		}

		// POST: /ProductCategory/Create
		// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
		// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Create([Bind(Include = "Id,ParentProductCategoryId,Name,Description")] ProductCategory productCategory)
		{
			if (!ModelState.IsValid) return View(productCategory);

			if (_handler.Create(ref productCategory) == ServiceResultEnum.Success)
			{
				return RedirectToAction("Index");
			}

			productCategory.ProductCategories = _handler.ReadAll();
			productCategory.ProductCategorySelectListItems =
				productCategory.ProductCategories.ToSelectListItems(category => category.Name, category => category.Id.ToString(), category => category.Id == productCategory.ParentProductCategoryId);

			return View(productCategory);
		}

		// GET: /ProductCategory/Edit/5
		public ActionResult Edit(Guid? id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			var productCategory = _handler.ReadOne(id.Value);
			if (productCategory == null)
			{
				return HttpNotFound();
			}
			productCategory.ProductCategories = _handler.ReadAll();
			productCategory.ProductCategorySelectListItems =
				productCategory.ProductCategories.ToSelectListItems(category => category.Name, category => category.Id.ToString(), category => category.Id == productCategory.ParentProductCategoryId);
			return View(productCategory);
		}

		// POST: /ProductCategory/Edit/5
		// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
		// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Edit([Bind(Include = "Id,ParentProductCategoryId,Name,Description")] ProductCategory productCategory)
		{
			if (!ModelState.IsValid) return View(productCategory);

			if (_handler.Update(ref productCategory) == ServiceResultEnum.Success)
			{
				return RedirectToAction("Index");
			}
			productCategory.ProductCategories = _handler.ReadAll();
			productCategory.ProductCategorySelectListItems =
				productCategory.ProductCategories.ToSelectListItems(category => category.Name, category => category.Id.ToString(), category => category.Id == productCategory.ParentProductCategoryId);
			return View(productCategory);
		}

		// GET: /ProductCategory/Delete/5
		public ActionResult Delete(Guid? id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			var store = _handler.ReadOne(id.Value);
			if (store == null)
			{
				return HttpNotFound();
			}
			return View(store);
		}

		// POST: /ProductCategory/Delete/5
		[HttpPost, ActionName("Delete")]
		[ValidateAntiForgeryToken]
		public ActionResult DeleteConfirmed(Guid id)
		{
			if (_handler.Delete(id) == ServiceResultEnum.Success)
			{
				return RedirectToAction("Index");
			}
			return View(_handler.ReadOne(id));
		}
	}
}
